package com.pl.itsense.gwt.clientzone.ui;

import net.sourceforge.htmlunit.corejs.javascript.ast.LetNode;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.pl.itsense.gwt.clientzone.client.Clientzone;
import com.pl.itsense.gwt.clientzone.model.EventType;
import com.pl.itsense.gwt.clientzone.model.MemberDTO;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.grid.CellFormatter;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.smartgwt.client.widgets.grid.events.CellDoubleClickEvent;
import com.smartgwt.client.widgets.grid.events.CellDoubleClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VStack;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripMenuButton;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;
import com.smartgwt.client.widgets.HTMLPane;
import com.smartgwt.client.widgets.Window;

public class WorkspacePanel extends Window{
	static WorkspacePanel instance = null;

	TabSet tabSet = new TabSet();  

	
	TreeNode treeNodeBazyDanych = new TreeNode("Bazy danych");
	TreeNode treeNodeHurtownieDanych = new TreeNode("Hurtownie danych");
	TreeNode treeNodeBusinessIntellignece = new TreeNode("Business intelligence");
	TreeNode treeNodeSystemyRaportowe = new TreeNode("Systemy raportowe");
	TreeNode treeNodeIntranet = new TreeNode("Intranet");
	TreeNode treeNodeObslugaInformatyczna = new TreeNode("Obsługa informatyczna");
	
	
	

	

	
	
	ToolStrip toolStrip = new ToolStrip();  
	
	
	
	
	public WorkspacePanel() {
		instance = this;
		init();
	}

	public static void hideInstance(){
		if (instance != null){
			instance.setVisible(false);
			instance.destroy();
			instance = null;
		}
	}

	public static void showInstance(){
		if (instance != null){
			instance.setVisible(true);
		}else{
			RootPanel.get().add( new WorkspacePanel());
			instance.setVisible(true);
		}
		
	}

	private void init(){
		setTitle("Strefa Klienta ITSense Honorata Bojar");
		setMaximized(true);
		setShowCloseButton(false);
		setShowMinimizeButton(false);
		setShowMaximizeButton(false);
		setCanDragReposition(false);
		setCanDragResize(false);
		
		TreeGrid treeGrid = new TreeGrid();  
		treeGrid.setWidth(150);  
		treeGrid.setShowConnectors(true);  
		   
		treeGrid.addCellDoubleClickHandler(new CellDoubleClickHandler() {
			@Override
			public void onCellDoubleClick(CellDoubleClickEvent event) {
				ListGridRecord rec = event.getRecord();
				TreeNode node = (TreeNode)rec;
				
				if (treeNodeBazyDanych == node){
					Tab tab = tabSet.getTab("databases");
					if (tab == null){
						tabSet.addTab(createTabCennikBazyDanych());
					}else{
					}
					return;
				}
				
				if (treeNodeBusinessIntellignece == node){
					Tab tab = tabSet.getTab("bi");
					if (tab == null){
						tabSet.addTab(createTabCennikBusinessIntelligence());
					}else{
					}
					return;
				}
				
				
				
				
			}
		});
		Tree data = new Tree();  
		
		data.setModelType(TreeModelType.CHILDREN);
		
		TreeNode cenniki = new TreeNode("Cenniki");
		
		
		
		TreeNode[] tabCenniki = new TreeNode[6];
		tabCenniki[0] = treeNodeBazyDanych;
		tabCenniki[1] = treeNodeHurtownieDanych;
		tabCenniki[2] = treeNodeBusinessIntellignece;
		tabCenniki[3] = treeNodeSystemyRaportowe;
		tabCenniki[4] = treeNodeIntranet;
		tabCenniki[5] = treeNodeObslugaInformatyczna;
		
		cenniki.setChildren( tabCenniki);


		
		TreeNode projects = new TreeNode("Moje projekty");
		projects.setEnabled(false);
		
		data.setRoot(new TreeNode("root",cenniki, projects));  
			
		treeGrid.setData(data);
		HLayout hLayout = new HLayout();
		hLayout.setWidth100();
		hLayout.setHeight100();
		
		treeGrid.setShowResizeBar(true);
		
		hLayout.setMembers(treeGrid, tabSet);
		
		
		
		ToolStripMenuButton mainMenuButton = getMainManu();
		ToolStripMenuButton toolMenuButton = getToolMenu();
		
		
		toolStrip.addMenuButton(mainMenuButton);
		toolStrip.addMenuButton(toolMenuButton);
		
		toolStrip.setWidth100();
		
		VStack vStack = new VStack();  
		vStack.addMember(toolStrip);
		vStack.addMember(hLayout);
		
		addItem(vStack);
		
	}

	
	private ToolStripMenuButton getMainManu() {  
		Menu menu = new Menu();  
		menu.setShowShadow(true);  
		menu.setShadowDepth(3);  
		MenuItem newItem = new MenuItem("Nowy projekt ...");  
		newItem.setEnabled(false);
		MenuItem exitItem = new MenuItem("Wyloguj", "exit-icon.png");
		exitItem.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(MenuItemClickEvent event) {
				Clientzone.clientZoneService.event(Clientzone.memberDTO.getId(), EventType.LOGOUT, "", new AsyncCallback<Boolean>() {
					@Override
					public void onSuccess(Boolean result) {
						Clientzone.setApplicationState(0);
					}
					
					@Override
					public void onFailure(Throwable caught) {
						Clientzone.setApplicationState(0);
					}
				});
				
				Clientzone.setApplicationState(0);
			}
		});
		
		menu.setItems(newItem, exitItem);  
		
		ToolStripMenuButton menuButton = new ToolStripMenuButton("Strefa klienta", menu);  
		menuButton.setWidth(100);  
		return menuButton;  
	}  

	private ToolStripMenuButton getToolMenu() {  
		Menu menu = new Menu();  
		menu.setShowShadow(true);  
		menu.setShadowDepth(3);  
		MenuItem monitorItem = new MenuItem("Monitor projektów", "process-icon.png");  
		MenuItem profileItem = new MenuItem("Mój profil", "user-icon.png");  
		monitorItem.setEnabled(false);

		MenuItem hasloItem = new MenuItem("Hasło");  
		MenuItem daneKontaktoweItem = new MenuItem("Dane kontaktowe");  
		MenuItem preferencjeItem = new MenuItem("Preferencje");  
		MenuItem usunKontoItem = new MenuItem("Usuń konto","Trash-Full-icon.png");  

		usunKontoItem.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(MenuItemClickEvent event) {
			SC.confirm("Czy napewno chcesz usunąć konto ?", new BooleanCallback() {
					@Override
					public void execute(Boolean value) {

						if (value){
							Clientzone.clientZoneService.removeAccount(Clientzone.memberDTO.getId(),new AsyncCallback<Boolean>(){
								@Override
								public void onFailure(Throwable caught) {
									SC.say("Wystąpił błąd podzcas usuwania Twoich danych z serwisu Strefa Kienta ITSense.", new BooleanCallback() {
										@Override
										public void execute(Boolean value) {
											Clientzone.setApplicationState(0);
										}
									});
								}

								@Override
								public void onSuccess(Boolean result) {
									SC.say("Twoje dane zostały usunięte z serwisu Strefa Kienta ITSense.", new BooleanCallback() {
										@Override
										public void execute(Boolean value) {
											Clientzone.setApplicationState(0);
										}
									});

								}
							}); 
						}
					} 
				});
				
			};
		});
		
		
		hasloItem.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(MenuItemClickEvent event) {
				ChangePasswordPanel panel = new ChangePasswordPanel();
				panel.show();
			}
		});
		
		
		daneKontaktoweItem.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(MenuItemClickEvent event) {
				ChangeDaneKontaktowePanel panel = new ChangeDaneKontaktowePanel();
				panel.show();
			}
		});
		
		preferencjeItem.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(MenuItemClickEvent event) {
				ChangePreferencjePanel panel = new ChangePreferencjePanel();
				panel.show();
			}
		});
		
		
		Menu menu2 = new Menu();  
		menu2.setItems(hasloItem,daneKontaktoweItem,preferencjeItem);
		profileItem.setSubmenu(menu2);
		
		profileItem.setEnabled(true);
		menu.setItems(monitorItem,profileItem, usunKontoItem);  
		
		ToolStripMenuButton menuButton = new ToolStripMenuButton("Narzędzia", menu);  
		menuButton.setWidth(100);  
		return menuButton;  
	}  
	

	private Tab createTabCennikBazyDanych(){
		Tab tab = new Tab("Baz danych - cennik");
		tab.setCanClose(true);
		tab.setID("databases");
		HTMLPane htmlPane = new HTMLPane();  
		htmlPane.setWidth100();  
		htmlPane.setHeight100();  
		htmlPane.setContents("<html><body><h1>Bazy Danych cennik </h1></body></html>");
		tab.setPane(htmlPane);
		return tab;
	}

	private Tab createTabCennikBusinessIntelligence(){
		Tab tab = new Tab("Business intelligence - cennik");
		tab.setCanClose(true);
		tab.setID("bi");
		HTMLPane htmlPane = new HTMLPane();  
		htmlPane.setWidth100();  
		htmlPane.setHeight100();  
		htmlPane.setContents("<html><body><h1>Business Intelligence cennik </h1></body></html>");
		tab.setPane(htmlPane);
		return tab;
	}

}

